using System;
using System.Text;
using UnityEngine;

public class MathHelper
{
	public const float minFloat = 1E-06f;

	public static StringBuilder str = new StringBuilder("0:00");

	public static LayerMask nothingLayer => 0;

	public static LayerMask everythingLayer => -1;

	public static Vector3 setOffsetXYZ(Vector3 vec3, float x, float y, float z)
	{
		return new Vector3(vec3.x + x, vec3.y + y, vec3.z + z);
	}

	public static Vector3 setX(Vector3 vec3, float x)
	{
		return new Vector3(x, vec3.y, vec3.z);
	}

	public static Vector3 setY(Vector3 vec3, float y)
	{
		return new Vector3(vec3.x, y, vec3.z);
	}

	public static Vector3 setZ(Vector3 vec3, float z)
	{
		return new Vector3(vec3.x, vec3.y, z);
	}

	public static Vector3 setOffestX(Vector3 vec3, float x)
	{
		return new Vector3(vec3.x + x, vec3.y, vec3.z);
	}

	public static Vector3 setOffestY(Vector3 vec3, float y)
	{
		return new Vector3(vec3.x, vec3.y + y, vec3.z);
	}

	public static Vector3 setOffestZ(Vector3 vec3, float z)
	{
		return new Vector3(vec3.x, vec3.y, vec3.z + z);
	}

	public static Vector2 vector3To2(Vector3 vec3)
	{
		return new Vector2(vec3.x, vec3.z);
	}

	public static Vector3 vector2To3(Vector2 vec2)
	{
		return new Vector3(vec2.x, 0f, vec2.y);
	}

	public static Vector3[] vector2ArrayTo3(Vector2[] vec2s)
	{
		Vector3[] array = new Vector3[vec2s.Length];
		for (int i = 0; i < vec2s.Length; i++)
		{
			array[i] = vector2To3(vec2s[i]);
		}
		return array;
	}

	public static float getSqrDistance(Vector3 vec1, Vector3 vec2)
	{
		return (vec1 - vec2).sqrMagnitude;
	}

	public static float getSqrDistance_vec2(Vector3 vec1, Vector3 vec2)
	{
		return (new Vector2(vec1.x, vec1.z) - new Vector2(vec2.x, vec2.z)).sqrMagnitude;
	}

	public static Vector3 geDirectionRightNormal(Vector3 direction)
	{
		return new Vector3(direction.z, 0f, 0f - direction.x).normalized;
	}

	public static Vector2 geDirectionRightNormal(Vector2 direction)
	{
		return new Vector2(direction.y, 0f - direction.x).normalized;
	}

	public static bool isDirectionAtDirecitonRight(Vector2 me, Vector2 other)
	{
		Vector2 lhs = new Vector2(me.y, 0f - me.x);
		return Vector2.Dot(lhs, other) > 0f;
	}

	public static bool isDirectionAtDirecitonRight(Vector3 me, Vector3 other)
	{
		Vector2 lhs = new Vector2(me.z, 0f - me.x);
		return Vector2.Dot(lhs, vector3To2(other)) > 0f;
	}

	public static bool isInRect(Rect rect, Vector2 pos)
	{
		if (pos.x >= rect.xMin && pos.x <= rect.xMax && pos.y >= rect.yMin && pos.y <= rect.yMax)
		{
			return true;
		}
		return false;
	}

	public static bool isInCircle(Vector3 origin, float r, Vector3 pos)
	{
		if (getSqrDistance(pos, origin) < r * r)
		{
			return true;
		}
		return false;
	}

	public static bool isInScreen(Vector2 pos)
	{
		if (pos.x > (float)Screen.width || pos.y > (float)Screen.height || pos.x < 0f || pos.y < 0f)
		{
			return false;
		}
		return true;
	}

	public static float max(float a, float b)
	{
		return (!(a > b)) ? b : a;
	}

	public static bool isTransfromAtTransformFront(Transform me, Transform other)
	{
		Vector3 lhs = other.position - me.position;
		return Vector3.Dot(lhs, me.forward) > 0f;
	}

	public static Color setAlpha(Color color, float alpha)
	{
		return new Color(color.r, color.g, color.b, alpha);
	}

	public static bool isPosInRectangle(Vector3 pos, Vector3[] vecs)
	{
		for (int i = 0; i < vecs.Length; i++)
		{
			if (i == vecs.Length - 1)
			{
				if (!isDirectionAtDirecitonRight(vecs[0] - vecs[i], pos - vecs[i]))
				{
					return false;
				}
			}
			else if (!isDirectionAtDirecitonRight(vecs[i + 1] - vecs[i], pos - vecs[i]))
			{
				return false;
			}
		}
		return true;
	}

	public static LayerMask getLayerMaskByLayerCount(int layerCount)
	{
		layerCount = Mathf.Clamp(layerCount, 0, 31);
		return Mathf.RoundToInt(Mathf.Pow(2f, layerCount));
	}

	public static Vector2 getRandomPosInRect(Rect rect)
	{
		float num = UnityEngine.Random.Range(0f, 1f);
		return new Vector2(rect.x, rect.y) * num + new Vector2(rect.xMax, rect.yMax) * (1f - num);
	}

	public static void calculateClipPlaneSize(Camera camera, float zPosInCameraSpace, out float clipPlaneHeight, out float clipPlaneWidth)
	{
		clipPlaneHeight = Mathf.Tan((float)Math.PI / 180f * camera.fieldOfView / 2f) * zPosInCameraSpace * 2f;
		clipPlaneWidth = clipPlaneHeight * camera.aspect;
	}

	public static T[] get2DArrayLine<T>(T[,] twoDArray, int lineNum)
	{
		if (twoDArray == null || lineNum < 0 || lineNum >= twoDArray.GetLength(0))
		{
			return null;
		}
		T[] array = new T[twoDArray.GetLength(1)];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = twoDArray[lineNum, i];
		}
		return array;
	}

	public static Vector2 scaleRange(Vector2 range, float scale)
	{
		float num = (range.x + range.y) * 0.5f;
		float num2 = Mathf.Abs(range.y - num);
		return new Vector2(num - scale * num2, num + scale * num2);
	}

	public static bool isFloatZero(float a)
	{
		return Mathf.Abs(a) < 1E-06f;
	}

	public static bool isFloatEqual(float a, float b)
	{
		return Mathf.Abs(a - b) < 1E-06f;
	}

	public static bool isVector2Equal(Vector2 v1, Vector2 v2)
	{
		return Mathf.Abs(v1.x - v2.x) < 1E-06f && Mathf.Abs(v1.y - v2.y) < 1E-06f;
	}

	public static bool isVector3Equal(Vector3 v1, Vector3 v2)
	{
		return Mathf.Abs(v1.x - v2.x) < 0.1f && Mathf.Abs(v1.y - v2.y) < 0.1f&& Mathf.Abs(v1.z - v2.z) < 0.1f;
	}

	public static Rect rectRelative2Real(Rect r)
	{
		return new Rect(r.x * (float)Screen.width, r.y * (float)Screen.height, r.width * (float)Screen.width, r.height * (float)Screen.height);
	}

	public static Vector3 convertPixelPosToTransformPos(Vector2 pos, float pos_Z)
	{
		return new Vector3(pos.x / 800f, pos.y / 480f, pos_Z);
	}

	public static Vector2 convertTransformPosToPixelPos(Vector3 pos)
	{
		return new Vector2(pos.x * 800f, pos.y * 480f);
	}

	public static int getMaxValueIndexInArray(float[] array)
	{
		if (array.Length == 0)
		{
			UnityEngine.Debug.LogError("array == null");
			return 0;
		}
		int result = 0;
		float num = array[0];
		for (int i = 1; i < array.Length; i++)
		{
			if (num < array[i])
			{
				num = array[i];
				result = i;
			}
		}
		return result;
	}

	public static int getMinValueIndexInArray(float[] array)
	{
		if (array.Length == 0)
		{
			UnityEngine.Debug.LogError("array == null");
			return 0;
		}
		int result = 0;
		float num = array[0];
		for (int i = 1; i < array.Length; i++)
		{
			if (num > array[i])
			{
				num = array[i];
				result = i;
			}
		}
		return result;
	}

	public static string floatToClockFormat(float time)
	{
		int value = (int)time / 60;
		int num = (int)time % 60;
		if (time <= 0f)
		{
			str.Remove(0, str.Length);
			str.Append("0:00");
			return str.ToString();
		}
		if (time <= 15f)
		{
			str.Remove(0, str.Length);
			str.Append(time.ToString("#0.0"));
		}
		else if (num < 10)
		{
			str.Remove(0, str.Length);
			str.Append(value);
			str.Append(":0");
			str.Append(num);
		}
		else
		{
			str.Remove(0, str.Length);
			str.Append(value);
			str.Append(":");
			str.Append(num);
		}
		return str.ToString();
	}

	public static bool isArrayHaveNum(int[] array, int num)
	{
		for (int i = 0; i < array.Length; i++)
		{
			if (num == array[i])
			{
				return true;
			}
		}
		return false;
	}

	public static int getArraySum(int[] array)
	{
		int num = 0;
		for (int i = 0; i < array.Length; i++)
		{
			num += array[i];
		}
		return num;
	}

	public static int getIndexInArray(int[] array, int num)
	{
		for (int i = 0; i < array.Length; i++)
		{
			if (num == array[i])
			{
				return i;
			}
		}
		return 0;
	}

	public static int[] getIndexInRankOrder(int[] array)
	{
		int[] array2 = new int[array.Length];
		for (int i = 0; i < array.Length; i++)
		{
			array2[i] = i;
		}
		for (int j = 0; j < array.Length; j++)
		{
			for (int k = 0; k < array.Length - j - 1; k++)
			{
				if (array[k] < array[k + 1])
				{
					int num = array[k];
					array[k] = array[k + 1];
					array[k + 1] = num;
					num = array2[k];
					array2[k] = array2[k + 1];
					array2[k + 1] = num;
				}
			}
		}
		return array2;
	}

	public static int[] setArrayToRandomOrder(int[] a)
	{
		int num = a.Length;
		for (int i = 0; i < num; i++)
		{
			int num2 = UnityEngine.Random.Range(0, num);
			if (num2 != i)
			{
				int num3 = a[i];
				a[i] = a[num2];
				a[num2] = num3;
			}
		}
		return a;
	}

	public static bool isInPercent(float percent)
	{
		return UnityEngine.Random.Range(0f, 100f) < percent;
	}

	public static void fitXPos(UITexture texture)
	{
		Transform transform = texture.transform;
		Vector3 localPosition = texture.transform.localPosition;
		Vector3 localPosition2 = texture.transform.localPosition;
		transform.localPosition = setX(localPosition, localPosition2.x * MyScreen.factor);
	}

	public static void fitXPos(UISprite sprite)
	{
		Transform transform = sprite.transform;
		Vector3 localPosition = sprite.transform.localPosition;
		Vector3 localPosition2 = sprite.transform.localPosition;
		transform.localPosition = setX(localPosition, localPosition2.x * MyScreen.factor);
	}
}
